package info.zhoutian.blog.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.Object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/8/24.
 */
@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

//    @Before("execution(public * info.zhoutian.blog.controller.*(..))")
//    public void log(){
//        System.out.println("......");
//    }
//
//    @After("execution(public * info.zhoutian.blog.controller.*(..))")
//    public void log2(){
//        System.out.println("......");
//
//    }
//
//    @Around("execution(public * info.zhoutian.blog.controller.*(..))")
//    public void log3(){
//        System.out.println("......");
//
//    }


    @Pointcut("execution(public * info.zhoutian.blog.controller.*(..))")
    public void log(){
    }

    @After("log()")
    public void log2(){
        logger.info("........");
        System.out.println("......");

    }

    @Before("log()")
    public void log3(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURL());
        //method
        logger.info("method={}",request.getMethod());
        //ip
        logger.info("ip={}",request.getRemoteAddr());
        //类方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringType() + "." + joinPoint.getSignature().getName());
        //参数
        logger.info("args={}",joinPoint.getArgs());



        System.out.println("......");

    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturingn(Object object){
        logger.info("response={}",object.toString());
    }



}
